package com.springmvc.web.majiang.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.springmvc.web.majiang.entity.Room;
import com.springmvc.web.majiang.entity.User;
import com.springmvc.web.majiang.service.IRoomService;
import com.springmvc.web.majiang.service.IUserService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WangRuChao
 * @since 2017-12-11
 */
@Controller
@RequestMapping("/room")
public class RoomController {

	@Autowired
	IRoomService roomService;

	@Autowired
	IUserService userService;

	@RequestMapping
	public String index(@RequestParam("userid") String userid, Model model, HttpServletRequest request,
			HttpServletResponse response) {
		List<Map<String, Object>> list = roomService.selectMaps(new EntityWrapper<Room>());

		for (Map<String, Object> map : list) {
			String roomuserid = String.valueOf(map.get("roomuserid"));
			User user = userService.selectById(roomuserid);
			if (user != null) {
				map.put("roomusername", user.getUsername());
			} else {
				map.put("roomusername", "");
			}
		}
		model.addAttribute("list", list);
		model.addAttribute("userid", userid);
		return "room/list";
	}

	@RequestMapping("/toadd")
	public String toadd(@RequestParam("userid") String userid, Model model, HttpServletRequest request,
			HttpServletResponse response) {
		model.addAttribute("userid", userid);
		return "room/add";
	}

	@RequestMapping("/add")
	public String toadd(@RequestParam("roomname") String roomname, @RequestParam("roomuserid") Long roomuserid,
			Model model, HttpServletRequest request, HttpServletResponse response) {
		Room entity = new Room();
		entity.setRoomname(roomname);
		entity.setCreatetime(new Date());
		entity.setUpdatetime(new Date());
		entity.setRoomuserid(roomuserid);
		roomService.insert(entity);
		model.addAttribute("userid", roomuserid);
		return "redirect:/room";
	}

	@RequestMapping("/join")
	public String join(@RequestParam("userid") Long userid, @RequestParam("roomid") Long roomid, Model model,
			HttpServletRequest request, HttpServletResponse response) {

		synchronized (roomid) {
			Wrapper<User> wrapper1 = new EntityWrapper<User>();
			wrapper1.eq("roomid", roomid);
			int position = userService.selectCount(wrapper1);

			Wrapper<User> wrapper = new EntityWrapper<User>();
			wrapper.eq("roomid", roomid);
			wrapper.eq("id", userid);
			User user = userService.selectOne(wrapper);
			if (user == null) {
				position = position + 1;
				User entity = new User();
				entity.setId(userid);
				entity.setRoomid(roomid);
				entity.setPosition(position);
				userService.updateById(entity);
			}

			user = userService.selectById(userid);

			model.addAttribute("userid", user.getId());
			model.addAttribute("roomid", user.getRoomid());
			model.addAttribute("position", user.getPosition());
		}

		return "majiang/index";
	}
}
